WEBVTT

00:00.700 --> 00:05.280
In this video you will learn how to create exponentially weighted moving averages.

00:05.440 --> 00:09.160
So in a minute you will see how it works and what the differences are.

00:09.160 --> 00:11.200
To a simple moving averages.

00:11.290 --> 00:14.850
But first of all let's import pandas and also met plot lip.

00:15.070 --> 00:19.130
And again we are working with the S&amp;P 500 data set.

00:19.210 --> 00:24.410
So we are importing the data from the S&amp;P 500 since we fire.

00:24.530 --> 00:25.860
Let's have a look here.

00:26.010 --> 00:30.430
And in this video we are only working with the last 10 years.

00:30.460 --> 00:40.630
So from 2 9 2 to 18 and we are actually here overriding our variable S&amp;P 500.

00:40.630 --> 00:42.820
So let's have a look again.

00:42.850 --> 00:50.240
So here our data frame starts at the very last day of the year 2000 and 8 and we've already learned

00:50.260 --> 00:54.030
how to create simple moving averages with the rolling method.

00:54.070 --> 01:00.910
So by defining the rolling window and this example 10 and changing the mean method panels calculates

01:00.970 --> 01:04.960
the rolling mean values which are actually equally weighted.

01:05.110 --> 01:10.390
So in each timestamp the immediately preceding 10 data points are equally weighted.

01:10.450 --> 01:12.780
And now this makes a lot of sense.

01:12.820 --> 01:18.250
However it might be the case that that we want to give more weight or a higher weight to more recent

01:18.250 --> 01:24.640
observations and that we can do this with an exponential weighting.

01:24.640 --> 01:30.460
So let's first of all calculate the simple moving average of the close column here with a rolling window

01:30.460 --> 01:31.120
of 10

01:35.050 --> 01:41.710
and actually the appropriate method to calculate exponentially away that moving averages is the E W

01:41.740 --> 01:48.130
M method and from a mathematical perspective there are plenty of options how to exponentially weight

01:48.160 --> 01:53.860
the observations and the parameters to do so span and half life.

01:53.860 --> 01:54.980
So let's have a look here.

01:57.040 --> 02:04.010
So you have the parameter span and half life and also alpha but actually I do not want to go here and

02:04.010 --> 02:11.600
to a mathematical details and therefore a quite popular way is to use a span.

02:11.600 --> 02:18.500
So here we have also the span parameter and here in the IWM method we actually pass the 10 to the span

02:18.500 --> 02:25.050
parameter and also the FAA the min periods parameter and we pass also the value of 10.

02:25.130 --> 02:31.430
So we need at least 10 observations to calculate an exponentially weighted moving average.

02:32.180 --> 02:35.810
And finally we change the mean method so let's have a look.

02:36.620 --> 02:40.540
So these are here the exponentially awaited moving averages.

02:41.180 --> 02:47.120
And now we can also create two new columns a simple moving average and exponentially weighted moving

02:47.120 --> 02:53.680
average and we actually append these new columns uh to our data frame S&amp;P 500.

02:54.050 --> 02:58.640
So let's do this here and let's have a look.

02:58.670 --> 03:01.090
So here we have our closing prices.

03:01.120 --> 03:06.590
And then the simple moving averages and the exponentially weighted moving averages.

03:06.590 --> 03:11.610
So yeah at the very end we can see we have uh nominal values here.

03:11.990 --> 03:15.160
And now the question is if and to what extent.

03:15.230 --> 03:21.410
Simple moving average is an exponentially weighted moving average just behave in a different way.

03:21.410 --> 03:27.240
And uh the best way to discover this is actually with a graph.

03:27.320 --> 03:31.520
So we're actually plotting here the last two columns.

03:31.520 --> 03:33.670
So we are selecting the last two columns.

03:33.690 --> 03:35.620
And use the plot method.

03:35.630 --> 03:36.710
So let's have a look here.

03:39.660 --> 03:43.340
And we can see here in blue will the line for simple moving averages.

03:43.380 --> 03:49.800
And in green for exponentially moving averages and since exponentially weighted average just place more

03:49.800 --> 03:52.710
weight on more recent observations.

03:52.710 --> 03:59.070
So the green graph actually adapts faster to change as compared to the equal weighted versions of here.

03:59.070 --> 04:00.030
The blue graph.

04:00.360 --> 04:08.550
So for example we can see here that the S&amp;P 500 declines and the it seems like that the green curve

04:08.640 --> 04:13.530
adapts to the declining prices faster than the blue curve.

04:13.530 --> 04:20.010
And also here we can see that exponentially weighted average US actually we are moving faster are adapting

04:20.010 --> 04:23.310
faster to most the recent trends.

04:23.310 --> 04:28.170
So this is the rationale behind exponentially weighted moving averages.

04:28.170 --> 04:31.440
And I hope to see you also in the next video by.
